package com.stox.client;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractBar;
import com.stox.dto.enumeration.SpanType;

@Component
public class CachingBarClient implements BarClient{

	@Autowired
	private BarClient barClient;
	
	private final Map<Long, Map<SpanType, List<AbstractBar>>> cache = new LinkedHashMap<Long, Map<SpanType, List<AbstractBar>>>(5){
		private static final long serialVersionUID = 1L;

		private final int cacheCapacity = 5;
		
		@Override
		protected boolean removeEldestEntry(Map.Entry<Long, Map<SpanType, List<AbstractBar>>> eldest) {
	        return size() >= cacheCapacity;
	    }
		
	};

	@Override
	public <T extends AbstractBar> List<T> findByScripId(Long scripId, SpanType spanType, Date start, int count) {
		return barClient.findByScripId(scripId, spanType, start, count);
	}
	
	@Override
	public <T extends AbstractBar> List<T> findByScripId(Long scripId, SpanType spanType, Date start,
			Date end) {
		return barClient.findByScripId(scripId, spanType, start, end);
	}
	
}
